<?php
/**
 * KvFramework base class
 * @package KvFramework_v2
 * @subpackage KvF_Base
 * @author Greg McWhirter <gsmcwhirter@gmail.com>
 * @copyright Copyright &copy; 2006, Greg McWhirter
 * @version $Revision: 25M $
 */

load_files(
  KVF_FRAMEWORK."/kvf_base/exceptions/kvf_base.exception.php",
  KVF_FRAMEWORK."/kvf_base/exceptions/kvf_struct.exception.php",
  KVF_FRAMEWORK."/kvf_base/kvf_struct.class.php",
  KVF_FRAMEWORK."/kvf_base/kvf_base_inflector.class.php",
  KVF_FRAMEWORK."/kvf_base/interfaces/kvf_dbhandler.interface.php",
  KVF_FRAMEWORK."/kvf_base/interfaces/kvf_loghandler.interface.php",
  KVF_FRAMEWORK."/kvf_base/interfaces/kvf_mailhandler.interface.php"
);

/**
 * Base class of the framework
 * @package KvFramework_v2
 * @subpackage KvF_Base
 */
abstract class KvfBase
{

  /**
   * Flag for whether or not the framework has been started
   *
   * @var boolean
   */
  private static $STARTED = false;

  /**
   * Input scrubbing
   *
   * @var NakorCore
   */
  private static $NAKOR_CORE;

  /**
   * Cleaned $_POST copy
   *
   * @var array
   */
  public static $POST = array();

  /**
   * Cleaned $_GET copy
   *
   * @var array
   */
  public static $GET = array();

  /**
   * $_SERVER copy
   *
   * @var array
   */
  public static $SERVER = array();

  /**
   * $_COOKIES copy
   *
   * @var array
   */
  public static $COOKIES = array();

  /**
   * $_SESSION copy
   *
   * @var array
   */
  public static $SESSION = array();

  /**
   * $_ENV copy
   *
   * @var array
   */
  public static $ENV = array();

  /**
   * $_FILES copy
   *
   * @var array
   */
  public static $FILES = array();

  /**
   * $_REQUEST copy
   *
   * @var array
   */
  public static $REQUEST = array();

  /**
   * Holds a merger of self::$GET, self::$POST, and some things the router finds
   *
   * @var array
   */
  public static $PARAMS = array();

  /**
   * Holds the database connection
   *
   * @var mixed
   */
  public static $DB = null;

  /**
   * Holds the errors and notices
   *
   * @var array
   */
  private static $MESSAGES = array(
    "errors" => array(),
    "notices" => array()
  );

  /**
   * An inflector instance
   *
   * @var KvfBaseInflector
   */
  public static $INFLECTOR;

  /**
   * The router instance
   *
   * @var KvfRouter
   */
  public static $ROUTER;

  /**
   * The mod_rewrite url redirection
   *
   * @var string
   */
  private static $KVFURL;

  /**
   * Holds parameters for use within layout files.
   *
   * @var array
   */
  protected static $LAYOUTPARAMS = array();

  /**
   * Startup the Framework
   *
   * @throws KvfBaseException
   */
  public static function RunApplication($_db, $_kvfuri)
  {
    /*See if this has already been called.*/
    if (self::$STARTED)
    {
      throw new KvfBaseException("Tried to call Kvfbase::RunApplication() more than once");
    }

    self::$STARTED = true;
    /* start the input scrubber */
    self::$NAKOR_CORE = new NakorCore();

    /* scrub input etc */
    self::$POST = self::$NAKOR_CORE->CleanInput("POST");
    self::$GET = self::$NAKOR_CORE->CleanInput("GET");
    self::$SERVER = $_SERVER;
    self::$COOKIES = $_COOKIE;
    //self::$SESSION = $_SESSION;
    self::$ENV = $_ENV;
    self::$FILES = $_FILES;
    self::$REQUEST = $_REQUEST;
    //self::$KVFURL = self::$GET["kvfurl"];
    self::$KVFURL = $_kvfuri;
    //unset(self::$GET["kvfurl"]);
    self::CleanArrayRecursive(self::$GET);
    self::CleanArrayRecursive(self::$POST);
    self::$PARAMS = array_merge(self::$GET, self::$POST);

    self::$INFLECTOR = new KvfBaseInflector();
    self::$ROUTER = KvfRouter::Instance(self::$KVFURL, self::$INFLECTOR);
    self::$DB = $_db;

    self::ReloadMessages();

    self::$ROUTER->DoRoute(self::$KVFURL);

  }

  /**
   * Reloads messages from session if available
   *
   */
  private static function ReloadMessages()
  {
    if(array_key_exists('kvf_messages', $_SESSION))
    {
      if(array_key_exists('errors', $_SESSION['kvf_messages']))
      {
        self::$MESSAGES["errors"] = $_SESSION['kvf_messages']["errors"];
      }
      if(array_key_exists('notices', $_SESSION['kvf_messages']))
      {
        self::$MESSAGES["notices"] = $_SESSION['kvf_messages']["notices"];
      }
    }
  }

  /**
   * Saves messages to the session
   *
   */
  public static function SaveMessages()
  {
    $_SESSION['kvf_messages'] = self::$MESSAGES;
  }

  /**
   * Save an error message
   *
   * @param string $message Message to save
   */
  public static function Error($message)
  {
    self::$MESSAGES["errors"][] = $message;
  }

  /**
   * Save a notice message
   *
   * @param string $message Message to save
   */
  public static function Notice($message)
  {
    self::$MESSAGES["notices"][] = $message;
  }

  /**
   * Determine whether there are errors or not
   *
   * @return boolean Whether or not there are errors
   */
  public static function IsErrors()
  {
    return(count(self::$MESSAGES["errors"]) > 0);
  }

  /**
   * Determine whether there are notices or not
   *
   * @return boolean Whether or not there are notices
   */
  public static function IsNotices()
  {
    return(count(self::$MESSAGES["notices"]) > 0);
  }

  /**
   * Returns the logged errors
   *
   * @return array
   */
  public static function GetErrors()
  {
    $t = self::$MESSAGES["errors"];
    self::$MESSAGES["errors"] = array();
    return $t;
  }

  /**
   * Returns the logged notices
   *
   * @return array
   */
  public static function GetNotices()
  {
    $t = self::$MESSAGES["notices"];
    self::$MESSAGES["notices"] = array();
    return $t;
  }

  /**
   * Cleans an array, making empty string values null
   */
  final public static function CleanArrayRecursive(&$array){
    array_walk_recursive($array, array(__CLASS__, "CleanCallback"));
  }

  /**
   * Used by clean_array_recursive as the callback
   */
  final public static function CleanCallback(&$r, $i){
    if($r == "") { $r = null; }
  }

  /**
   * Commits the current session
   *
   */
  /*public static function CommitSession()
  {
    global $SESSIONSTARTED;
    if($SESSIONSTARTED)
    {
      $_SESSION = array();
      foreach(self::$SESSION as $k => $v)
      {
        $_SESSION[$k] = $v;
      }
      //session_commit();
      //session_start();
      //$SESSIONSTARTED = false;
    }
    return;
  }*/

  /**
   * Wrapper around the cache system in case it is not being used
   *
   * @param string $_cachekey
   * @return mixed
   */
  public static function GetCache($_cachekey)
  {
    if(class_exists("KvfCache"))
    {
      return KvfCache::GetCache($_cachekey);
    }
    else
    {
      return null;
    }
  }

  /**
   * Wrapper around the cache system in case it is not being used
   *
   * @param string $_cachekey
   * @param mixed $_cachevalue
   * @param integer $_cacheto
   * @return boolean
   */
  public static function SaveCache($_cachekey, $_cachevalue, $_cacheto = null)
  {
    if(class_exists("KvfCache"))
    {
      return KvfCache::SaveCache($_cachekey, $_cachevalue, (is_null($_cacheto)) ? CONFIG_CACHE::TIMEOUT : $_cacheto);
    }
    else
    {
      return false;
    }
  }

  /**
   * Wrapper around the cache system in case it is not being used
   *
   * @param string $_cachekey
   * @return boolean
   */
  public static function ExpireCache($_cachekey)
  {
    if(class_exists("KvfCache"))
    {
      return KvfCache::ExpireCache($_cachekey);
    }
    else
    {
      return true;
    }
  }

  /**
   * Wrapper to get the KVFURL of the request
   *
   * @return string
   */
  public static function GetKvfUrl()
  {
    return self::$KVFURL;
  }
}

?>
